package com.leji.song.extra;


import android.app.Activity;
import android.app.LocalActivityManager;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Shader.TileMode;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.TabHost;
import android.widget.TabHost.OnTabChangeListener;
import android.widget.TextView;
import com.leji.song.R;
import com.leji.song.mainPage.MainPageActivity;
import com.leji.song.mine.MineActivity;
import com.leji.song.myOrder.MyOrderActivity;

public class MainTabActivity extends Activity {
    /** Called when the activity is first created. */
	String[] title = new String[]{"首页","我的订单","我的"};
	View Tab1,Tab2,Tab3;
	View[] tabs = new View[]{Tab1, Tab2,Tab3};

	TextView newActions_1, newActions_2, newActions_3, newActions_4, newActions_5;
	TextView[] newActions = new TextView[5];
//	ImageView[] newActions = new ImageView[]{newActions_1, newActions_2, newActions_3, newActions_4, newActions_5};
	
	BitmapDrawable bd;
	TabHost tabHost;
	
	private LocalActivityManager lam;
	private long exitTime;


    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.tab_content);


        tabHost = (TabHost)findViewById(R.id.tabhost);
        tabHost.setup();   //Call setup() before adding tabs if loading TabHost using findViewById().
        
        lam = new LocalActivityManager(MainTabActivity.this, false);
        lam.dispatchCreate(savedInstanceState);
 
        tabHost.setup(lam);
//        tabHost.setup(this.getLocalActivityManager());   
        
//		Bitmap bitmap1 = BitmapFactory.decodeResource(this.getResources(), R.drawable.none);
//		bd = new BitmapDrawable(bitmap1);
//		bd.setTileModeXY(TileMode.REPEAT , TileMode.CLAMP );
//		bd.setDither(true);
		
		Intent intent0 = new Intent(this, MainPageActivity.class);
		
		Intent intent1 = new Intent(this, MyOrderActivity.class);
		
		Intent intent2 = new Intent(this, MineActivity.class);

		Intent[] intentArray = new Intent[]{intent0, intent1, intent2};
		
		Drawable drawable1 = getResources().getDrawable(R.drawable.maintab_selector_0);
		Drawable drawable2 = getResources().getDrawable(R.drawable.maintab_selector_1);
		Drawable drawable3 = getResources().getDrawable(R.drawable.maintab_selector_2);

		Drawable[] iconList = new Drawable[]{drawable1, drawable2, drawable3};
		
        for(int i=0;i<tabs.length;i++){
        	tabs[i] = LayoutInflater.from(this).inflate(R.layout.tabmini, null);
  
        	//tabs[i].setBackgroundDrawable(bd);
        	
            tabHost.addTab(tabHost.newTabSpec(title[i]).setIndicator(tabs[i]).setContent(intentArray[i]));
            
            TextView text = (TextView) tabs[i].findViewById(R.id.tab_label);
            View tab_bg = tabs[i].findViewById(R.id.tab_bg);
            View tab_forcusbg = tabs[i].findViewById(R.id.tab_forcusbg);
            ImageView tab_icon = (ImageView) tabs[i].findViewById(R.id.tab_icon);
//            newActions[i] = (TextView) tabs[i].findViewById(R.id.newActions);
            
//            tab_bg.setBackgroundDrawable(bd);
//            tab_forcusbg.setBackgroundDrawable(getResources().getDrawable(R.drawable.maintab_fg_selector));

            tab_icon.setBackgroundDrawable(iconList[i]);
            text.setText(title[i]); 
       
        }
        

        		
        tabHost.setCurrentTab(0);
        
        tabHost.setOnTabChangedListener(new OnTabChangeListener() {
			@Override
			public void onTabChanged(String tabId) {

	        }		
		});
        

    }


    @Override
	protected void onDestroy() {
//		unregisterReceiver(mBroadcastReceiver);
		super.onDestroy();
	}

//	@Override
//	public boolean onKeyDown(int keyCode, KeyEvent event) {
//		if (keyCode == KeyEvent.KEYCODE_BACK ) {
//	        if((System.currentTimeMillis() - exitTime) > 2000){
//	            showToast("再按一次退出掌上学苑 ");
//	            exitTime = System.currentTimeMillis();
//	        } else {
//	        	finish();
//	        }
//		}
//
//		return true;
//	}

	
	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
	         // 获取当前活动的Activity实例
	         Activity subActivity = lam.getCurrentActivity();
	         //判断是否实现返回值接口
	         if (subActivity instanceof OnTabActivityResultListener) {
	             //获取返回值接口实例
	             OnTabActivityResultListener listener = (OnTabActivityResultListener) subActivity;
	             //转发请求到子Activity
	             listener.onTabActivityResult(requestCode, resultCode, data);
	         }
	         super.onActivityResult(requestCode, resultCode, data);
	     }


}

